//
//  AlertControllerTools.m
//  AlertControllerDemo
//
//  Created by wei wang on 2017/10/14.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "AlertControllerTools.h"

@implementation AlertControllerTools

+ (UIAlertController *)alertControllerWithTitle:(NSString *)promptTitle message:(NSString *)promptMessage alertControllerType:(UIAlertControllerStyle)style buttonArray:(NSArray *)buttons buttonClick:(AlertButtonClick)buttonCallBack {
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:promptTitle message:promptMessage preferredStyle:style];
    
    for ( NSInteger idx = 0; idx < buttons.count; ++idx ) {
        NSString *actionTitle = buttons[idx];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if( buttonCallBack ) {
                buttonCallBack(action.actionIndex);
            }
        }];
        
        alertAction.actionIndex = idx;
        [alertControl addAction:alertAction];
    }
    
    return alertControl;
}

+ (UIAlertController *)alertControllerWithTitle:(NSString *)promptTitle message:(NSString *)promptMessage alertControllerType:(UIAlertControllerStyle)style buttonArray:(NSArray *)buttons buttonClick:(AlertButtonClick)buttonCallBack showInViewCroller:(UIViewController *)viewController {
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:promptTitle message:promptMessage preferredStyle:style];
    
    for ( NSInteger idx = 0; idx < buttons.count; ++idx ) {
        NSString *actionTitle = buttons[idx];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if( buttonCallBack ) {
                buttonCallBack(action.actionIndex);
            }
        }];
        
        alertAction.actionIndex = idx;
        [alertControl addAction:alertAction];
    }
    
    [viewController presentViewController:alertControl animated:YES completion:nil];
    
    return alertControl;
}




@end
